/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlparser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author peto
 */
public final class HttpConnectionReader {
   
        
        URL serverAddress = null;
        HttpURLConnection connection = null;
        final int READ_TIME_OUT = 8000;
        
        
        /**
         * Constructs a URL connection to the specified URL. 
         * A connection to the object referenced by the URL is not created.
         * 
         * @param url server address
         * @throws MalformedURLException if is server addres invalid.
         */
        public HttpConnectionReader(String url) throws MalformedURLException{
            setServerAddress(url);
        }
        
        
        
        
        /**
         * Opens a communications link to the resource referenced by this URL, 
         * if such a connection has not already been established.
         * 
         * @throws IOException if an I/O error occurs while opening the connection.
         * @throws IllegalArgumentException  If was server response different as 200.
         */
        
        public void connect() throws IOException, IllegalArgumentException{
            
            if(serverAddress == null){
                throw new IllegalArgumentException("Server addres is not set.");
            }
           
            connection = (HttpURLConnection) serverAddress.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(READ_TIME_OUT);
            connection.setUseCaches(true);
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.connect();
            if (connection.getResponseCode() != 200){
                throw new IllegalArgumentException("Connection to " + serverAddress.toString() + " failed");
            }   
            
        }
        
        /**
         * Returns an input stream that reads from this open connection.
         * 
         * @return InputStream from given url, if an I/O error not occurs, otherwise null.
         */
        public InputStream getHttpInputStream(){
            try {
                return connection.getInputStream();
            } catch (IOException ex) {
                Logger.getLogger(HttpConnectionReader.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
        
        /**
         * Set server address to http connect.
         * 
         * @param url URL
         * @throws MalformedURLException if is address invalid
         */
        public void setServerAddress(String url) throws MalformedURLException{
                serverAddress = new URL(url);
        }
        
        /**
         * Return url of server to connect.
         * 
         * @return String server address. 
         */
        public String getServerAddress(){
            return serverAddress.toString();
        }
        
        /**
         * Current Http Connection
         * 
         * @return HttpURLConnection 
         */
        public HttpURLConnection getHttpConnection(){
            return connection;
        }
        
        /**
         * Indicates that other requests to the server are unlikely in the near future. 
         * Calling disconnect() <strong>should not</strong> imply that this 
         * HttpURLConnection instance can be reused for other requests.
         * 
         */
        public void disconnect(){
            if(connection != null){
                connection.disconnect();
                serverAddress = null;
                connection = null;
            }
        }
}
